package com.fancy.mobileschool.service;


import android.os.Environment;

import com.fancy.mobileschool.entity.Course;
import com.fancy.mobileschool.entity.YunFile;
import com.fancy.mobileschool.util.OKHttpUtil;


import java.io.File;

import java.util.List;
import okhttp3.OkHttpClient;

/**
 * Created by Jackie on 2017/11/12.
 *
 */

public class YunFileService {

    private static final String baseUrl = URLConfig.baseUrl +  "yun_file";

    private static final String baseUrlForPerson = baseUrl + "/person/";

    private static final String baseUrlForCourse = baseUrl + "/course/";

    public List<YunFile> getMyYunFiles(String myID) {
        List<YunFile> rs;
        String url = baseUrl + "/getMyFile.do?myID=" + myID;
        rs = OKHttpBase.fromJsonListByGet(url, YunFile.class);

        return rs;
    }

    public List<Course> getShareCourse() {
        List<Course> rs ;
        String url = baseUrlForCourse + "getShareCourse.do";
        rs = OKHttpBase.fromJsonListByGet(url, Course.class);
        return rs;
    }

    public List<Course> getMyCourses() {
        return getShareCourse();
    }

    public List<YunFile> getCourseShareFiles(String course_name) {
        List<YunFile> rs;
        String url = baseUrlForCourse + "getCourseShareFiles.do?course_name=" + course_name;
        rs = OKHttpBase.fromJsonListByGet(url, YunFile.class);
        return rs;
    }

    public boolean uploadPersonFile(File file, String myID) {
        String url = baseUrlForPerson + "uploadFile.do?myID=" + myID;
        return OKHttpBase.uploadFile(url, file);
    }

    public boolean uploadCourseFile(File file, String course_id, String myID) {
        String url = baseUrlForCourse + "uploadFile.do?course_name=" + course_id + "&myID=" + myID;
        return OKHttpBase.uploadFile(url, file);
    }

    //如果下载成功返回文件名
    public String downLoadPersonFile(String myID, String fileName) {
        File file = Environment.getExternalStorageDirectory();
        String fileLocation = file.getAbsolutePath()  + "/" + fileName;
        String url = baseUrl + "/personFileDownLoad.do?myID=" + myID + "&fileName=" + fileName;
        return OKHttpBase.downLoadFile(url, fileLocation);
    }

}
